let initialState = {
    token: '',
    adminname:'',
    keyPath:'',
    // 侧边导航展开收起时的状态
    collapsed:true,
}

export default function reducer(state = initialState, action: any) {
    // 1. 深拷贝
    let newstate = JSON.parse(JSON.stringify(state))

    // 2.判断action类型
    // token
    if (action.type == 'SAVE_TOKEN') {
        newstate.token = action.payload
    } else if (action.type == 'REMOVE_TOKEN') {
        newstate.token = ''
    }
    // adminname
    if (action.type == 'SAVE_ADMINNAME') {
        newstate.adminname = action.payload
    } else if (action.type == 'REMOVE_ADMINNAME') {
        newstate.adminname = ''
    }
    // keyPath
    if (action.type == 'SAVE_KEYPATH') {
        newstate.keyPath = action.payload.reverse()
    } else if (action.type == 'REMOVE_KEYPATH') {
        newstate.keyPath = ''
    }
    // collapsed
    if (action.type == 'SAVE_COLLAPSED') {
        newstate.collapsed = action.payload
    } else if (action.type == 'REMOVE_COLLAPSED') {
        newstate.collapsed = ''
    }

    // 3.返回全新的state
    return newstate
}